﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Runtime.InteropServices;
using System.Xml;
using System.Xml.XPath;

public partial class Admin_ManageMenu : System.Web.UI.Page
{
    string xmlpath = "~/WebMenu.xml";
    string webpath = "~/Admin/ManageMenu.aspx";


    TreeNode tn = new TreeNode();
    TreeNode tnp1 = new TreeNode();
    TreeNode tnp2 = new TreeNode();
    TreeNode tnp3 = new TreeNode();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

        }
    }

    //添加菜单
    protected void BtnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            if (this.nName.Value != "")//判断菜单名称是否为空
            {
                tn = TreeView1.SelectedNode;
                ViewState["GetText"] = tn.Text;

                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(Server.MapPath(xmlpath));
                //通过读取菜单路径中的"/"判断是第几层节点
                int i = tn.ValuePath.Split('/').Length - 1;
                int c = 0;
                for (int j = 0; j < i; j++)
                {
                    c = c + 1;
                }

                string inVis;
                string owner = ",管理员,";//权限默认是管理员
                if (this.nVis.Checked == true)//判断是否显示
                {
                    inVis = "1";
                }
                else
                {
                    inVis = "0";
                }
                XmlElement nodeE = xmldoc.CreateElement("node");
                //设置需要添加的节点的标示和各项属性
                nodeE.SetAttribute("caption", this.nName.Value);
                nodeE.SetAttribute("visible", inVis);
                //如果序号没有填写，则添加一个默认的序号“0”
                if (this.selectedvalue.Value == "")
                {
                    nodeE.SetAttribute("order", "0");
                }
                else
                {
                    nodeE.SetAttribute("order", this.selectedvalue.Value);
                }
                nodeE.SetAttribute("owner", owner);
                nodeE.SetAttribute("FileName", this.selecteUrl.Value);
                int x = 0;
                int y = 0;
                int z = 0;
                int u = 0;
                bool w = true;
                string AlertAdd = "同一父菜单下的同级子菜单不允许有相同的名称";

                //为ROOT添加子节点
                if (c == 0)
                {
                    if (xmldoc.ChildNodes[1].HasChildNodes)//判断ROOT下是否存在子节点
                    {
                        for (x = 0; x < xmldoc.ChildNodes[1].ChildNodes.Count; x++)
                        {
                            if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[x].Attributes.GetNamedItem("caption").Value) == this.nName.Value)//判断同一父节点下的同级子节点是否有相同名称的节点
                            {
                                w = false;
                                break;
                            }
                        }
                        if (w == true)
                        {
                            if (this.nName.Value != "")
                            {
                                xmldoc.ChildNodes[1].AppendChild(nodeE);//添加节点
                                xmldoc.Save(Server.MapPath(xmlpath));//保存xml文件
                                Response.Redirect(webpath);//重新加载页面
                            }
                        }
                    }
                    else
                    {
                        if (this.nName.Value != "")
                        {
                            xmldoc.ChildNodes[1].AppendChild(nodeE);
                            xmldoc.Save(Server.MapPath(xmlpath));
                            Response.Redirect(webpath);
                        }
                    }
                }

                //为ROOT/node添加子节点
                else if (c == 1)
                {
                    for (x = 0; x < xmldoc.ChildNodes[1].ChildNodes.Count; x++)
                    {
                        if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[x].Attributes.GetNamedItem("caption").Value) == (string)ViewState["GetText"])
                        {
                            if (xmldoc.ChildNodes[1].ChildNodes[x].HasChildNodes)
                            {
                                for (y = 0; y < xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes.Count; y++)
                                {
                                    if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].Attributes.GetNamedItem("caption").Value) == this.nName.Value)
                                    {
                                        w = false;
                                        break;
                                    }
                                }
                                if (w == true)
                                {
                                    if (this.nName.Value != "")
                                    {
                                        xmldoc.ChildNodes[1].ChildNodes[x].AppendChild(nodeE);
                                        xmldoc.Save(Server.MapPath(xmlpath));
                                        Response.Redirect(webpath);
                                    }
                                }
                            }
                            else
                            {
                                if (this.nName.Value != "")
                                {
                                    xmldoc.ChildNodes[1].ChildNodes[x].AppendChild(nodeE);
                                    xmldoc.Save(Server.MapPath(xmlpath));
                                    Response.Redirect(webpath);
                                }
                            }
                        }
                    }
                }

                //为ROOT/node/node添加子节点
                else if (c == 2)
                {
                    tnp1 = TreeView1.SelectedNode.Parent;

                    ViewState["GetTextp1"] = tnp1.Text;

                    for (x = 0; x < xmldoc.ChildNodes[1].ChildNodes.Count; x++)
                    {
                        if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[x].Attributes.GetNamedItem("caption").Value) == (string)ViewState["GetTextp1"])
                        {
                            for (y = 0; y < xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes.Count; y++)
                            {
                                if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].Attributes.GetNamedItem("caption").Value) == (string)ViewState["GetText"])
                                {
                                    if (xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].HasChildNodes)
                                    {
                                        for (z = 0; z < xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes.Count; z++)
                                        {
                                            if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes[z].Attributes.GetNamedItem("caption").Value) == this.nName.Value)
                                            {
                                                w = false;
                                                break;
                                            }
                                        }
                                        if (w == true)
                                        {
                                            if (this.nName.Value != "")
                                            {
                                                xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].AppendChild(nodeE);
                                                xmldoc.Save(Server.MapPath(xmlpath));
                                                Response.Redirect(webpath);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (this.nName.Value != "")
                                        {
                                            xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].AppendChild(nodeE);
                                            xmldoc.Save(Server.MapPath(xmlpath));
                                            Response.Redirect(webpath);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                //为ROOT/node/node/node添加子节点
                else if (c == 3)
                {
                    tnp1 = TreeView1.SelectedNode.Parent;
                    tnp2 = TreeView1.SelectedNode.Parent.Parent;

                    ViewState["GetTextp1"] = tnp1.Text;
                    ViewState["GetTextp2"] = tnp2.Text;

                    for (x = 0; x < xmldoc.ChildNodes[1].ChildNodes.Count; x++)
                    {
                        if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[x].Attributes.GetNamedItem("caption").Value) == (string)ViewState["GetTextp2"])
                        {
                            for (y = 0; y < xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes.Count; y++)
                            {
                                if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].Attributes.GetNamedItem("caption").Value) == (string)ViewState["GetTextp1"])
                                {
                                    for (z = 0; z < xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes.Count; z++)
                                    {
                                        if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes[z].Attributes.GetNamedItem("caption").Value) == (string)ViewState["GetText"])
                                        {
                                            if (xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes[z].HasChildNodes)
                                            {
                                                for (u = 0; u < xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes[z].ChildNodes.Count; u++)
                                                {
                                                    if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes[z].ChildNodes[u].Attributes.GetNamedItem("caption").Value) == this.nName.Value)
                                                    {
                                                        w = false;
                                                        break;
                                                    }
                                                }
                                                if (w == true)
                                                {
                                                    if (this.nName.Value != "")
                                                    {
                                                        xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes[u].AppendChild(nodeE);
                                                        xmldoc.Save(Server.MapPath(xmlpath));
                                                        Response.Redirect(webpath);
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                if (this.nName.Value != "")
                                                {
                                                    xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes[u].AppendChild(nodeE);
                                                    xmldoc.Save(Server.MapPath(xmlpath));
                                                    Response.Redirect(webpath);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                else
                {
                    this.Lab1.Text = "本系统不允许在本级菜单下添加子菜单";
                }

                if (w == false)
                {
                    this.Lab1.Text = AlertAdd;
                }
            }
        }

        catch (Exception)
        {
            throw;
        }
    }
    //编辑菜单
    protected void BtnUpd_Click(object sender, EventArgs e)
    {
        try
        {
            if (this.nName.Value != "")
            {
                tn = TreeView1.SelectedNode;

                ViewState["GetText"] = tn.Text;

                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(Server.MapPath(xmlpath));
                int i = tn.ValuePath.Split('/').Length - 1;
                int c = 0;
                for (int j = 0; j < i; j++)
                {
                    c = c + 1;
                }

                string inVis;
                string owner = ",管理员,";
                string order = "0";
                if (this.nVis.Checked == true)
                {
                    inVis = "1";
                }
                else
                {
                    inVis = "0";
                }
                if (this.selectedvalue.Value == "")
                {
                    order = "0";
                }
                else
                {
                    order = this.selectedvalue.Value;
                }
                XmlElement nodeE = xmldoc.CreateElement("node");
                int x = 0;
                int y = 0;
                int z = 0;
                int u = 0;
                int v = 0;
                int w = 0;
                string AlertAdd = "同一父菜单下的同级子菜单不允许有相同的名称";

                if (c == 0)
                {
                    xmldoc.ChildNodes[1].Attributes["caption"].Value = this.nName.Value;
                    xmldoc.ChildNodes[1].Attributes["visible"].Value = inVis;
                    xmldoc.ChildNodes[1].Attributes["order"].Value = order;
                    xmldoc.ChildNodes[1].Attributes["owner"].Value = owner;
                    xmldoc.ChildNodes[1].Attributes["FileName"].Value = this.selecteUrl.Value;
                    xmldoc.Save(Server.MapPath(xmlpath));
                    Response.Redirect(webpath);
                }

                else if (c == 1)
                {
                    for (x = 0; x < xmldoc.ChildNodes[1].ChildNodes.Count; x++)
                    {
                        if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[x].Attributes.GetNamedItem("caption").Value) == (string)ViewState["GetText"])
                        {
                            for (v = 0; v < xmldoc.ChildNodes[1].ChildNodes.Count; v++)
                            {
                                //判断同一父节点下的同级子节点中是否有与需要编辑的节点名称相同的，有则w+1，如果只有一个，那么就是编辑后的节点没有改变名称，如果大于一个，说明存在与需要编辑的节点名称相同的节点，则不允许修改
                                if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[v].Attributes.GetNamedItem("caption").Value) == this.nName.Value)
                                {
                                    w = w + 1;
                                }
                            }
                            if (w <= 1)
                            {
                                xmldoc.ChildNodes[1].ChildNodes[x].Attributes["caption"].Value = this.nName.Value;
                                xmldoc.ChildNodes[1].ChildNodes[x].Attributes["visible"].Value = inVis;
                                xmldoc.ChildNodes[1].ChildNodes[x].Attributes["order"].Value = this.selectedvalue.Value;
                                xmldoc.ChildNodes[1].ChildNodes[x].Attributes["owner"].Value = owner;
                                xmldoc.ChildNodes[1].ChildNodes[x].Attributes["FileName"].Value = this.selecteUrl.Value;
                                xmldoc.Save(Server.MapPath(xmlpath));
                                Response.Redirect(webpath);
                            }
                        }
                    }
                }

                else if (c == 2)
                {
                    tnp1 = TreeView1.SelectedNode.Parent;

                    ViewState["GetTextp1"] = tnp1.Text;

                    for (x = 0; x < xmldoc.ChildNodes[1].ChildNodes.Count; x++)
                    {
                        if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[x].Attributes.GetNamedItem("caption").Value) == (string)ViewState["GetTextp1"])
                        {
                            for (y = 0; y < xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes.Count; y++)
                            {
                                if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].Attributes.GetNamedItem("caption").Value) == (string)ViewState["GetText"])
                                {
                                    for (v = 0; v < xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes.Count; v++)
                                    {
                                        if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[v].Attributes.GetNamedItem("caption").Value) == this.nName.Value)
                                        {
                                            w = w + 1;
                                        }
                                    }
                                    if (w <= 1)
                                    {
                                        xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].Attributes["caption"].Value = this.nName.Value;
                                        xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].Attributes["visible"].Value = inVis;
                                        xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].Attributes["order"].Value = this.selectedvalue.Value;
                                        xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].Attributes["owner"].Value = owner;
                                        xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].Attributes["FileName"].Value = this.selecteUrl.Value;
                                        xmldoc.Save(Server.MapPath(xmlpath));
                                        Response.Redirect(webpath);
                                    }
                                }
                            }
                        }
                    }
                }

                else if (c == 3)
                {
                    tnp1 = TreeView1.SelectedNode.Parent;
                    tnp2 = TreeView1.SelectedNode.Parent.Parent;

                    ViewState["GetTextp1"] = tnp1.Text;
                    ViewState["GetTextp2"] = tnp2.Text;

                    for (x = 0; x < xmldoc.ChildNodes[1].ChildNodes.Count; x++)
                    {
                        if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[x].Attributes.GetNamedItem("caption").Value) == (string)ViewState["GetTextp2"])
                        {
                            for (y = 0; y < xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes.Count; y++)
                            {
                                if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].Attributes.GetNamedItem("caption").Value) == (string)ViewState["GetTextp1"])
                                {
                                    for (z = 0; z < xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes.Count; z++)
                                    {
                                        if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes[z].Attributes.GetNamedItem("caption").Value) == (string)ViewState["GetText"])
                                        {
                                            for (v = 0; v < xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes.Count; v++)
                                            {
                                                if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes[v].Attributes.GetNamedItem("caption").Value) == this.nName.Value)
                                                {
                                                    w = w + 1;
                                                }
                                            }
                                            if (w <= 1)
                                            {
                                                xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes[z].Attributes["caption"].Value = this.nName.Value;
                                                xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes[z].Attributes["visible"].Value = inVis;
                                                xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes[z].Attributes["order"].Value = this.selectedvalue.Value;
                                                xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes[z].Attributes["owner"].Value = owner;
                                                xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes[z].Attributes["FileName"].Value = this.selecteUrl.Value;
                                                xmldoc.Save(Server.MapPath(xmlpath));
                                                Response.Redirect(webpath);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }


                else if (c == 4)
                {
                    tnp1 = TreeView1.SelectedNode.Parent;
                    tnp2 = TreeView1.SelectedNode.Parent.Parent;
                    tnp3 = TreeView1.SelectedNode.Parent.Parent.Parent;

                    ViewState["GetTextp1"] = tnp1.Text;
                    ViewState["GetTextp2"] = tnp2.Text;
                    ViewState["GetTextp3"] = tnp3.Text;

                    for (x = 0; x < xmldoc.ChildNodes[1].ChildNodes.Count; x++)
                    {
                        if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[x].Attributes.GetNamedItem("caption").Value) == (string)ViewState["GetTextp3"])
                        {
                            for (y = 0; y < xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes.Count; y++)
                            {
                                if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].Attributes.GetNamedItem("caption").Value) == (string)ViewState["GetTextp2"])
                                {
                                    for (z = 0; z < xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes.Count; z++)
                                    {
                                        if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes[z].Attributes.GetNamedItem("caption").Value) == (string)ViewState["GetTextp1"])
                                        {
                                            for (u = 0; u < xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes[z].ChildNodes.Count; u++)
                                            {
                                                if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes[z].ChildNodes[u].Attributes.GetNamedItem("caption").Value) == (string)ViewState["GetText"])
                                                {
                                                    for (v = 0; v < xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes[z].ChildNodes.Count; v++)
                                                    {
                                                        if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes[z].ChildNodes[v].Attributes.GetNamedItem("caption").Value) == this.nName.Value)
                                                        {
                                                            w = w + 1;
                                                        }
                                                    }
                                                    if (w <= 1)
                                                    {
                                                        xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes[z].ChildNodes[u].Attributes["caption"].Value = this.nName.Value;
                                                        xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes[z].ChildNodes[u].Attributes["visible"].Value = inVis;
                                                        xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes[z].ChildNodes[u].Attributes["order"].Value = this.selectedvalue.Value;
                                                        xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes[z].ChildNodes[u].Attributes["owner"].Value = owner;
                                                        xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes[z].ChildNodes[u].Attributes["FileName"].Value = this.selecteUrl.Value;
                                                        xmldoc.Save(Server.MapPath(xmlpath));
                                                        Response.Redirect(webpath);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                }

                if (w > 1)
                {
                    this.Lab1.Text = AlertAdd;
                }
            }
        }
        catch (Exception)
        {
            throw;
        }
    }
    //删除菜单
    protected void BtnDel_Click(object sender, EventArgs e)
    {
        try
        {
            if (this.nName.Value != "")
            {
                tn = TreeView1.SelectedNode;

                ViewState["GetText"] = tn.Text;

                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(Server.MapPath(xmlpath));
                int i = tn.ValuePath.Split('/').Length - 1;
                int c = 0;
                for (int j = 0; j < i; j++)
                {
                    c = c + 1;
                }
                XmlElement nodeE = xmldoc.CreateElement("node");
                string AlertAdd2 = "此菜单存在子菜单，禁止删除";
                int x = 0;
                int y = 0;
                int z = 0;
                int u = 0;
                bool w = true;

                if (c == 0)
                {
                    this.Lab1.Text = "根节点不允许删除";
                }

                else if (c == 1)
                {
                    for (x = 0; x < xmldoc.ChildNodes[1].ChildNodes.Count; x++)
                    {
                        if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[x].Attributes.GetNamedItem("caption").Value) == (string)ViewState["GetText"])
                        {
                            if (xmldoc.ChildNodes[1].ChildNodes[x].HasChildNodes)//如果需要删除的节点存在子节点则不允许删除
                            {
                                w = false;
                                break;
                            }
                            else
                            {
                                xmldoc.ChildNodes[1].RemoveChild(xmldoc.ChildNodes[1].ChildNodes[x]);
                                xmldoc.Save(Server.MapPath(xmlpath));
                                Response.Redirect(webpath);
                            }
                        }
                    }
                }

                else if (c == 2)
                {
                    tnp1 = TreeView1.SelectedNode.Parent;

                    ViewState["GetTextp1"] = tnp1.Text;

                    for (x = 0; x < xmldoc.ChildNodes[1].ChildNodes.Count; x++)
                    {
                        if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[x].Attributes.GetNamedItem("caption").Value) == (string)ViewState["GetTextp1"])
                        {
                            for (y = 0; y < xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes.Count; y++)
                            {
                                if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].Attributes.GetNamedItem("caption").Value) == (string)ViewState["GetText"])
                                {
                                    if (xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].HasChildNodes)
                                    {
                                        w = false;
                                        break;
                                    }
                                    else
                                    {
                                        xmldoc.ChildNodes[1].ChildNodes[x].RemoveChild(xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y]);
                                        xmldoc.Save(Server.MapPath(xmlpath));
                                        Response.Redirect(webpath);
                                    }
                                }
                            }
                        }
                    }
                }

                else if (c == 3)
                {
                    tnp1 = TreeView1.SelectedNode.Parent;
                    tnp2 = TreeView1.SelectedNode.Parent.Parent;

                    ViewState["GetTextp1"] = tnp1.Text;
                    ViewState["GetTextp2"] = tnp2.Text;

                    for (x = 0; x < xmldoc.ChildNodes[1].ChildNodes.Count; x++)
                    {
                        if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[x].Attributes.GetNamedItem("caption").Value) == (string)ViewState["GetTextp2"])
                        {
                            for (y = 0; y < xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes.Count; y++)
                            {
                                if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].Attributes.GetNamedItem("caption").Value) == (string)ViewState["GetTextp1"])
                                {
                                    for (z = 0; z < xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes.Count; z++)
                                    {
                                        if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes[z].Attributes.GetNamedItem("caption").Value) == (string)ViewState["GetText"])
                                        {
                                            if (xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes[z].HasChildNodes)
                                            {
                                                w = false;
                                                break;
                                            }
                                            else
                                            {
                                                xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].RemoveChild(xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes[z]);
                                                xmldoc.Save(Server.MapPath(xmlpath));
                                                Response.Redirect(webpath);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                else if (c == 4)
                {
                    tnp1 = TreeView1.SelectedNode.Parent;
                    tnp2 = TreeView1.SelectedNode.Parent.Parent;
                    tnp3 = TreeView1.SelectedNode.Parent.Parent.Parent;

                    ViewState["GetTextp1"] = tnp1.Text;
                    ViewState["GetTextp2"] = tnp2.Text;
                    ViewState["GetTextp3"] = tnp3.Text;

                    for (x = 0; x < xmldoc.ChildNodes[1].ChildNodes.Count; x++)
                    {
                        if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[x].Attributes.GetNamedItem("caption").Value) == (string)ViewState["GetTextp3"])
                        {
                            for (y = 0; y < xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes.Count; y++)
                            {
                                if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].Attributes.GetNamedItem("caption").Value) == (string)ViewState["GetTextp2"])
                                {
                                    for (z = 0; z < xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes.Count; z++)
                                    {
                                        if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes[z].Attributes.GetNamedItem("caption").Value) == (string)ViewState["GetTextp1"])
                                        {
                                            for (u = 0; u < xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes[z].ChildNodes.Count; u++)
                                            {
                                                if (Convert.ToString(xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes[z].ChildNodes[u].Attributes.GetNamedItem("caption").Value) == (string)ViewState["GetText"])
                                                {
                                                    if (xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes[z].ChildNodes[u].HasChildNodes)
                                                    {
                                                        w = false;
                                                        break;
                                                    }
                                                    else
                                                    {
                                                        xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes[z].RemoveChild(xmldoc.ChildNodes[1].ChildNodes[x].ChildNodes[y].ChildNodes[z].ChildNodes[u]);
                                                        xmldoc.Save(Server.MapPath(xmlpath));
                                                        Response.Redirect(webpath);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                if (w == false)
                {
                    this.Lab1.Text = AlertAdd2;
                }
            }
        }
        catch (Exception)
        {
            throw;
        }

    }
    //菜单排序
    protected void BtnSort_Click(object sender, EventArgs e)
    {
        try
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(Server.MapPath(xmlpath));
            int i = 0;
            int j = 0;
            int k = 0;
            int l = 0;
            int m = 0;

            for (i = 0; i < xmldoc.ChildNodes[1].ChildNodes.Count; i++)
            {
                if (xmldoc.ChildNodes[1].ChildNodes[i].HasChildNodes)
                {
                    for (j = 0; j < xmldoc.ChildNodes[1].ChildNodes[i].ChildNodes.Count; j++)
                    {
                        if (xmldoc.ChildNodes[1].ChildNodes[i].ChildNodes[j].HasChildNodes)
                        {
                            for (k = 0; k < xmldoc.ChildNodes[1].ChildNodes[i].ChildNodes[j].ChildNodes.Count; k++)
                            {
                                if (xmldoc.ChildNodes[1].ChildNodes[i].ChildNodes[j].ChildNodes[k].HasChildNodes)
                                {
                                    for (l = 0; l < xmldoc.ChildNodes[1].ChildNodes[i].ChildNodes[j].ChildNodes[k].ChildNodes.Count; l++)
                                    {
                                        for (m = 0; m < xmldoc.ChildNodes[1].ChildNodes[i].ChildNodes[j].ChildNodes[k].ChildNodes.Count - 1; m++)
                                        {
                                            if (Convert.ToDouble(xmldoc.ChildNodes[1].ChildNodes[i].ChildNodes[j].ChildNodes[k].ChildNodes[m].Attributes.GetNamedItem("order").Value) > Convert.ToDouble(xmldoc.ChildNodes[1].ChildNodes[i].ChildNodes[j].ChildNodes[k].ChildNodes[m + 1].Attributes.GetNamedItem("order").Value))
                                            {
                                                xmldoc.ChildNodes[1].ChildNodes[i].ChildNodes[j].ChildNodes[k].InsertBefore(xmldoc.ChildNodes[1].ChildNodes[i].ChildNodes[j].ChildNodes[k].ChildNodes[m + 1], xmldoc.ChildNodes[1].ChildNodes[i].ChildNodes[j].ChildNodes[k].ChildNodes[m]);
                                            }
                                        }
                                    }
                                }

                            }
                        }
                    }
                }
            }

            for (i = 0; i < xmldoc.ChildNodes[1].ChildNodes.Count; i++)
            {
                if (xmldoc.ChildNodes[1].ChildNodes[i].HasChildNodes)
                {
                    for (j = 0; j < xmldoc.ChildNodes[1].ChildNodes[i].ChildNodes.Count; j++)
                    {
                        if (xmldoc.ChildNodes[1].ChildNodes[i].ChildNodes[j].HasChildNodes)
                        {
                            for (k = 0; k < xmldoc.ChildNodes[1].ChildNodes[i].ChildNodes[j].ChildNodes.Count; k++)
                            {
                                for (l = 0; l < xmldoc.ChildNodes[1].ChildNodes[i].ChildNodes[j].ChildNodes.Count - 1; l++)
                                {
                                    if (Convert.ToDouble(xmldoc.ChildNodes[1].ChildNodes[i].ChildNodes[j].ChildNodes[l].Attributes.GetNamedItem("order").Value) > Convert.ToDouble(xmldoc.ChildNodes[1].ChildNodes[i].ChildNodes[j].ChildNodes[l + 1].Attributes.GetNamedItem("order").Value))
                                    {
                                        xmldoc.ChildNodes[1].ChildNodes[i].ChildNodes[j].InsertBefore(xmldoc.ChildNodes[1].ChildNodes[i].ChildNodes[j].ChildNodes[l + 1], xmldoc.ChildNodes[1].ChildNodes[i].ChildNodes[j].ChildNodes[l]);
                                    }

                                }
                            }
                        }
                    }
                }
            }

            for (i = 0; i < xmldoc.ChildNodes[1].ChildNodes.Count; i++)
            {
                if (xmldoc.ChildNodes[1].ChildNodes[i].HasChildNodes)
                {
                    for (j = 0; j < xmldoc.ChildNodes[1].ChildNodes[i].ChildNodes.Count; j++)
                    {
                        for (k = 0; k < xmldoc.ChildNodes[1].ChildNodes[i].ChildNodes.Count - 1; k++)
                        {
                            if (Convert.ToDouble(xmldoc.ChildNodes[1].ChildNodes[i].ChildNodes[k].Attributes.GetNamedItem("order").Value) > Convert.ToDouble(xmldoc.ChildNodes[1].ChildNodes[i].ChildNodes[k + 1].Attributes.GetNamedItem("order").Value))
                            {
                                xmldoc.ChildNodes[1].ChildNodes[i].InsertBefore(xmldoc.ChildNodes[1].ChildNodes[i].ChildNodes[k + 1], xmldoc.ChildNodes[1].ChildNodes[i].ChildNodes[k]);
                            }

                        }
                    }
                }
            }

            for (i = 0; i < xmldoc.ChildNodes[1].ChildNodes.Count; i++)
            {
                for (j = 0; j < xmldoc.ChildNodes[1].ChildNodes.Count - 1; j++)
                {
                    if (Convert.ToDouble(xmldoc.ChildNodes[1].ChildNodes[j].Attributes.GetNamedItem("order").Value) > Convert.ToDouble(xmldoc.ChildNodes[1].ChildNodes[j + 1].Attributes.GetNamedItem("order").Value))
                    {
                        xmldoc.ChildNodes[1].InsertBefore(xmldoc.ChildNodes[1].ChildNodes[j + 1], xmldoc.ChildNodes[1].ChildNodes[j]);
                    }
                }
            }

            xmldoc.Save(Server.MapPath(xmlpath));
            Response.Redirect(webpath);
        }
        catch (Exception)
        {
            throw;
        }

    }
    //显示选中内容
    protected void TreeView1_SelectedNodeChanged1(object sender, EventArgs e)
    {
        tn = TreeView1.SelectedNode;

        ViewState["GetText"] = tn.Text;
        ViewState["GetNavigateUrl"] = tn.Target.ToString();
        ViewState["GetVis"] = tn.ImageToolTip.ToString();
        ViewState["GetOrder"] = tn.ToolTip;
        ViewState["NodePath"] = tn.ValuePath;
        string path = "/node";
        string rootpath = "ROOT";
        int i = tn.ValuePath.Split('/').Length - 1;
        int b = 0;
        for (int j = 0; j < i; j++)
        {
            rootpath = rootpath + path;
            b = b + 1;
        }
        ViewState["NodePath"] = rootpath;

        this.nName.Value = (string)ViewState["GetText"];
        this.selecteUrl.Value = (string)ViewState["GetNavigateUrl"];
        this.selectedvalue.Value = (string)ViewState["GetOrder"];
        //this.selectedvalue.Value = (string)ViewState["NodePath"];
        string vis = (string)ViewState["GetVis"];
        if (vis == "1")
        {
            this.nVis.Checked = true;
        }
        else if (vis == "0")
        {
            this.nVis.Checked = false;
        }

    }
}
